package com.gitee.wsl.compose.ui.base

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomAppBarDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.DockedSearchBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MediumTopAppBar
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.NavigationRailDefaults
import androidx.compose.material3.SearchBar
import androidx.compose.material3.SearchBarColors
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.material3.contentColorFor
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp

object Bar{
    object Item
}

@ExperimentalMaterial3Api
@Composable
fun Bar.topAppBarState(
    initialHeightOffsetLimit: Float = -Float.MAX_VALUE,
    initialHeightOffset: Float = 0f,
    initialContentOffset: Float = 0f
) = rememberTopAppBarState(initialHeightOffsetLimit, initialHeightOffset, initialContentOffset)


@ExperimentalMaterial3Api
@Composable
fun Bar.TopApp(
    title: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    navigationIcon: @Composable () -> Unit = {},
    actions: @Composable RowScope.() -> Unit = {},
    expandedHeight: Dp = TopAppBarDefaults.TopAppBarExpandedHeight,
    windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
    colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(),
    scrollBehavior: TopAppBarScrollBehavior? = null
) = TopAppBar(
    title,
    modifier,
    navigationIcon,
    actions,
    expandedHeight,
    windowInsets,
    colors,
    scrollBehavior
)


@ExperimentalMaterial3Api
@Composable
fun Bar.CenterAlignedTopApp(
    title: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    navigationIcon: @Composable () -> Unit = {},
    actions: @Composable RowScope.() -> Unit = {},
    expandedHeight: Dp = TopAppBarDefaults.TopAppBarExpandedHeight,
    windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
    colors: TopAppBarColors = TopAppBarDefaults.centerAlignedTopAppBarColors(),
    scrollBehavior: TopAppBarScrollBehavior? = null
) = CenterAlignedTopAppBar(
    title,
    modifier,
    navigationIcon,
    actions,
    expandedHeight,
    windowInsets,
    colors,
    scrollBehavior
)

@ExperimentalMaterial3Api
@Composable
fun Bar.MediumTopApp(
    title: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    navigationIcon: @Composable () -> Unit = {},
    actions: @Composable RowScope.() -> Unit = {},
    collapsedHeight: Dp = TopAppBarDefaults.MediumAppBarCollapsedHeight,
    expandedHeight: Dp = TopAppBarDefaults.MediumAppBarExpandedHeight,
    windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
    colors: TopAppBarColors = TopAppBarDefaults.mediumTopAppBarColors(),
    scrollBehavior: TopAppBarScrollBehavior? = null
) = MediumTopAppBar(
    title,
    modifier,
    navigationIcon,
    actions,
    collapsedHeight,
    expandedHeight,
    windowInsets,
    colors,
    scrollBehavior
)

@ExperimentalMaterial3Api
@Composable
fun Bar.LargeTopApp(
     title: @Composable () -> Unit,
     modifier: Modifier = Modifier,
     navigationIcon: @Composable () -> Unit = {},
     actions: @Composable RowScope.() -> Unit = {},
     collapsedHeight: Dp = TopAppBarDefaults.LargeAppBarCollapsedHeight,
     expandedHeight: Dp = TopAppBarDefaults.LargeAppBarExpandedHeight,
     windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
     colors: TopAppBarColors = TopAppBarDefaults.largeTopAppBarColors(),
     scrollBehavior: TopAppBarScrollBehavior? = null
 ) = LargeTopAppBar(
    title,
    modifier,
    navigationIcon,
    actions,
    collapsedHeight,
    expandedHeight,
    windowInsets,
    colors,
    scrollBehavior
)


@Composable
fun Bar.BottomApp(
    actions: @Composable RowScope.() -> Unit,
    modifier: Modifier = Modifier,
    floatingActionButton: @Composable (() -> Unit)? = null,
    containerColor: Color = BottomAppBarDefaults.containerColor,
    contentColor: Color = contentColorFor(containerColor),
    tonalElevation: Dp = BottomAppBarDefaults.ContainerElevation,
    contentPadding: PaddingValues = BottomAppBarDefaults.ContentPadding,
    windowInsets: WindowInsets = BottomAppBarDefaults.windowInsets,
) = BottomAppBar(
    actions,
    modifier,
    floatingActionButton,
    containerColor,
    contentColor,
    tonalElevation,
    contentPadding,
    windowInsets
)

@Composable
fun Bar.BottomApp(
    modifier: Modifier = Modifier,
    containerColor: Color = BottomAppBarDefaults.containerColor,
    contentColor: Color = contentColorFor(containerColor),
    tonalElevation: Dp = BottomAppBarDefaults.ContainerElevation,
    contentPadding: PaddingValues = BottomAppBarDefaults.ContentPadding,
    windowInsets: WindowInsets = BottomAppBarDefaults.windowInsets,
    content: @Composable RowScope.() -> Unit
) = BottomAppBar(
    modifier,
    containerColor,
    contentColor,
    tonalElevation,
    contentPadding,
    windowInsets,
    content
)

@Composable
fun Bar.Navigation(
    modifier: Modifier = Modifier,
    containerColor: Color = NavigationBarDefaults.containerColor,
    contentColor: Color = MaterialTheme.colorScheme.contentColorFor(containerColor),
    tonalElevation: Dp = NavigationBarDefaults.Elevation,
    windowInsets: WindowInsets = NavigationBarDefaults.windowInsets,
    content: @Composable RowScope.() -> Unit
) = NavigationBar(
    modifier,
    containerColor,
    contentColor,
    tonalElevation,
    windowInsets,
    content
)

@Composable
fun Bar.NavigationRail(
    modifier: Modifier = Modifier,
    containerColor: Color = NavigationRailDefaults.ContainerColor,
    contentColor: Color = contentColorFor(containerColor),
    header: @Composable (ColumnScope.() -> Unit)? = null,
    windowInsets: WindowInsets = NavigationRailDefaults.windowInsets,
    content: @Composable ColumnScope.() -> Unit
) = androidx.compose.material3.NavigationRail(
    modifier,
    containerColor,
    contentColor,
    header,
    windowInsets,
    content
)

@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun Bar.Search(query: String,
               onQueryChange: (String) -> Unit,
               onSearch: (String) -> Unit,
               active: Boolean,
               onActiveChange: (Boolean) -> Unit,
               modifier: Modifier = Modifier,
               enabled: Boolean = true,
               placeholder: @Composable (() -> Unit)? = null,
               leadingIcon: @Composable (() -> Unit)? = null,
               trailingIcon: @Composable (() -> Unit)? = null,
               shape: Shape = SearchBarDefaults.inputFieldShape,
               colors: SearchBarColors = SearchBarDefaults.colors(),
               tonalElevation: Dp = SearchBarDefaults.TonalElevation,
               shadowElevation: Dp = SearchBarDefaults.ShadowElevation,
               windowInsets: WindowInsets = SearchBarDefaults.windowInsets,
               interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
               content: @Composable ColumnScope.() -> Unit,)
 = SearchBar(
    inputField = {
        SearchBarDefaults.InputField(
            query = query,
            onQueryChange = onQueryChange,
            onSearch = onSearch,
            expanded = active,
            onExpandedChange = onActiveChange,
            enabled = enabled,
            placeholder = placeholder,
            leadingIcon = leadingIcon,
            trailingIcon = trailingIcon,
            colors = colors.inputFieldColors,
            interactionSource = interactionSource,
        )
    },
    expanded = active,
    onExpandedChange = onActiveChange,
    modifier = modifier,
    shape = shape,
    colors = colors,
    tonalElevation = tonalElevation,
    shadowElevation = shadowElevation,
    windowInsets = windowInsets,
    content = content,
)

@ExperimentalMaterial3Api
@Composable
fun Bar.DockedSearch(
    query: String,
    onQueryChange: (String) -> Unit,
    onSearch: (String) -> Unit,
    active: Boolean,
    onActiveChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    shape: Shape = SearchBarDefaults.dockedShape,
    colors: SearchBarColors = SearchBarDefaults.colors(),
    tonalElevation: Dp = SearchBarDefaults.TonalElevation,
    shadowElevation: Dp = SearchBarDefaults.ShadowElevation,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    content: @Composable ColumnScope.() -> Unit,
) = DockedSearchBar(
    inputField = {
        SearchBarDefaults.InputField(
            query = query,
            onQueryChange = onQueryChange,
            onSearch = onSearch,
            expanded = active,
            onExpandedChange = onActiveChange,
            enabled = enabled,
            placeholder = placeholder,
            leadingIcon = leadingIcon,
            trailingIcon = trailingIcon,
            colors = colors.inputFieldColors,
            interactionSource = interactionSource,
        )
    },
    expanded = active,
    onExpandedChange = onActiveChange,
    modifier = modifier,
    shape = shape,
    colors = colors,
    tonalElevation = tonalElevation,
    shadowElevation = shadowElevation,
    content = content,
)